﻿/*********************************************************************************/
/**    Sims3Pack Readme Generator                                               **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/




using System.Collections.Generic;
using System.Windows.Forms;
using Sims3PackReadmeGenerator.Layout;
using Sims3PackReadmeGenerator.language;

namespace Sims3PackReadmeGenerator.Gui
{
    /// <summary>
    /// form for creating new and editing existing layout collections
    /// for standard layout collections you can only change the language id (used when exporting the readme)
    /// </summary>
    public partial class EditLayoutCollectionGui : Form
    {

        #region properties

        public ILayoutCollection LayoutCollection { get; set; }

        #endregion

        #region constructor

        /// <summary>
       /// initialises the form
       /// for standard layout collections, the name textbox and language combobox are not enabled
       /// </summary>
       /// <param name="layoutCollection"></param>
       public EditLayoutCollectionGui(ILayoutCollection layoutCollection)
       {
           InitializeComponent();
           initLanguageComboBox();

           if (layoutCollection == null)
           {
               this.LayoutCollection = new CustomLayoutCollection();
           }
           else
           {
               this.LayoutCollection = layoutCollection;
               txtLayoutCollectionName.Text = layoutCollection.ToString();
               txtLanguageExtension.Text = layoutCollection.SaveFileIdCode;
               selectLanguageInCombobox(layoutCollection.LayoutLanguage);
           }

           checkforStandardLayoutCollection();
           
       }
        /// <summary>
        /// initialises the form
        /// </summary>
       public EditLayoutCollectionGui():this(null)
       {
          
       }

        #endregion

        #region methods
       
        /// <summary>
        /// Selects the fitting LayoutLanguageItem in the combobox for a given language
        /// </summary>
        /// <param name="lang">language</param>
        private void selectLanguageInCombobox(Languages lang)
       {
           int index = 0;
           foreach (LayoutLanguageItem item in cbLayoutCollectionLanguage.Items)
           {
               if (item.Language == lang)
               {
                   cbLayoutCollectionLanguage.SelectedIndex = index;
                   return;
               }
               index++;
           }
       }

        /// <summary>
        /// checks if collection is a standardlayoutcollection
        /// then the name textbox and the language combobox are disabled
        /// because these attributes of a standard collection are not meant to be changed
        /// </summary>
       private void checkforStandardLayoutCollection()
       {
           if (this.LayoutCollection.GetType() == typeof(StandardLayoutCollection))
           {
               txtLayoutCollectionName.Enabled = false;
               cbLayoutCollectionLanguage.Enabled = false;
           }
       }
       
        /// <summary>
        /// Fills the combobox which displays the avaible layout languages
        /// only German and English are supported at the moment
        /// </summary>
        private void initLanguageComboBox()
       {
           List<LayoutLanguageItem> langList = new List<LayoutLanguageItem>();
           langList.Add(new LayoutLanguageItem(Languages.de));
           langList.Add(new LayoutLanguageItem(Languages.en));
           cbLayoutCollectionLanguage.DataSource = langList;
           cbLayoutCollectionLanguage.SelectedIndex = 0;
       }

       /// <summary>
       /// handles the form closing event
       /// there must be text in the name and extension textboxes otherwise you can't close the form with ok
       /// when the form is closed with ok, the values from the gui are saved in the layout collection object
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void EditLayoutCollectionGui_FormClosing(object sender, FormClosingEventArgs e)
       {
           if (this.DialogResult == DialogResult.OK)
           {
               if (txtLayoutCollectionName.Text == "")
               {
                   errorProvider.Clear();
                   errorProvider.SetError(txtLayoutCollectionName, errors.errorMissingNameLayoutCollection);
                   e.Cancel = true;

               }
               else if (txtLanguageExtension.Text == "")
               {
                   errorProvider.Clear();
                   errorProvider.SetError(txtLanguageExtension, errors.errorMissingLangExtensionLayoutCol);
                   e.Cancel = true;
               }
               else
               {
                   this.LayoutCollection.SaveFileIdCode = txtLanguageExtension.Text;

                   if (txtLayoutCollectionName.Enabled && cbLayoutCollectionLanguage.Enabled)
                   {
                       this.LayoutCollection.Name = txtLayoutCollectionName.Text;
                       this.LayoutCollection.LayoutLanguage = ((LayoutLanguageItem)cbLayoutCollectionLanguage.SelectedItem).Language;
                   }

                   this.LayoutCollection.Save();
               }
           }
       }

        #endregion
    }
}
